package com.xshang.service.impl;

import com.xshang.entity.TenantDs;
import com.xshang.event.SaveTenantEvent;
import com.xshang.mapper.TenantMapper;
import com.xshang.service.TenantService;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class TenantServiceImpl implements TenantService {

    @Resource
    private ApplicationEventPublisher applicationEventPublisher;

    @Resource
    private TenantMapper tenantMapper;

    @Override
    public List<TenantDs> findAll() {
        return tenantMapper.findAll();
    }

    @Override
    public void save(TenantDs tenantDs) {
        tenantMapper.save(tenantDs);
        System.out.println("新增成功.. id 为:" + tenantDs.getTenantId());

        // 发布新增租户事件
        applicationEventPublisher
                .publishEvent(new SaveTenantEvent(this,
                        String.valueOf(tenantDs.getTenantId()),
                        tenantDs.getDsName()));
    }
}
